//
//  MLWCookBookViewController.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWCookBookViewController.h"
#import <SDCycleScrollView.h>
#import "MLWWWCyscrollModel.h"
#import "MLWWWHotTableViewCell.h"
#import "MLWWWFourImageView.h"
#import "MLWWWHotModel.h"
#import "MLWWWHotItemModel.h"
#import "MLWLXReadModel.h"
#import "MLWLXReadTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "MLWLXEventTableViewCell.h"
#import "MLWLXEventModel.h"
#import "MLWLXPersonTableViewCell.h"
#import "MLWLXPersonModel.h"
#import "MLWLXSmallPersonModel.h"
#import "MLWAlbumViewController.h"
#import "MLWPersonViewController.h"
#import "MLWReadViewController.h"
#import "MLWEventViewController.h"
#import "MLWVipViewController.h"
@interface MLWCookBookViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>

/** 轮播图数据源数组 */
@property (nonatomic, retain) NSMutableArray *scrollDataSource;

/** 轮播图图片数组 */
@property (nonatomic, retain) NSMutableArray *urlImageArr;

/** 轮播图文字数组 */
@property (nonatomic, retain) NSMutableArray *urlTextArr;

/** 主内容集合视图 */
@property (nonatomic, retain) UITableView *mainTableView;

/** hotModel */
@property (nonatomic, retain) MLWWWHotModel *hotModel;

/** 猜你喜欢 model */
@property (nonatomic, retain) MLWLXPersonModel *personModel;
@property (nonatomic, retain) NSMutableArray *perArray;
/** 定义阅读界面的model */
@property (nonatomic, retain) MLWLXReadModel *readModel;

/** 定义热门活动界面的model */
@property (nonatomic, retain) MLWLXEventModel *eventModel;

@property(nonatomic, retain) UIButton *rightButton;

/** 猜你喜欢 */
@property (nonatomic, retain) UICollectionView *personCollectionView;
@end

@implementation MLWCookBookViewController

-(void)dealloc
{
    self.mainTableView.dataSource = nil;
    self.mainTableView.delegate = nil;
    [_mainTableView release];
    
    [super dealloc];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @"KITCHEN STORIES";
    
    [self createMainTableView];
    [self getDataSource];
    
    
    
}
#pragma mark --- 主集合视图初始化
-(void)createMainTableView
{
    //数组初始化
    
    self.scrollDataSource = [NSMutableArray array];
    
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49)  style:UITableViewStyleGrouped];
    
    self.mainTableView.backgroundColor = [UIColor whiteColor];
    
    self.mainTableView.dataSource = self;
    
    self.mainTableView.delegate = self;
    
    //隐藏分割线
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.mainTableView.showsVerticalScrollIndicator = NO;
    
    //获取头视图
     [self getScorllView];
    
    
    //注册
    [self.mainTableView registerClass:[MLWWWHotTableViewCell class] forCellReuseIdentifier:@"mLWWWHotTableViewCell"];
    
    [self.mainTableView registerClass:[MLWLXPersonTableViewCell class] forCellReuseIdentifier:@"mlwLXPersonTableViewCell"];
      
    [self.mainTableView registerClass:[MLWLXReadTableViewCell class] forCellReuseIdentifier:@"mlwReadTableViewCell"];
    
    [self.mainTableView registerClass:[MLWLXEventTableViewCell class] forCellReuseIdentifier:@"mlwLXEventTableViewCell"];
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"txt"];
    
    [self.view addSubview:_mainTableView];
    [_mainTableView release];
    
}

#pragma mark --- 获取头视图
-(void)getScorllView
{
    //设置头视图
    
    NSMutableArray *scollarray = [NSMutableArray array];
    
    for (MLWWWCyscrollModel *model in self.scrollDataSource) {
        [scollarray addObject:model.Img];
    }
    
  
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth / 1.5) delegate: self placeholderImage:[UIImage imageNamed:@"Main_placeholder"]];
  
    cycleScrollView.imageURLStringsGroup = scollarray;
    
    self.mainTableView.tableHeaderView = cycleScrollView;
    
}

#pragma mark --- 返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}



#pragma mark --- 返回分区内cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //热门专辑
    if (section == 0) {
        return 1;
    }
    //猜你喜欢
    if (section == 1) {
        return 1;
    }
    //精品阅读
    if (section == 2) {
        return self.readModel.smallModelArray.count;
    }
    //热门活动
    if (section == 3) {
        return self.eventModel.smallModelArray.count;
    }
    //好豆达人
    if (section == 4) {
        return 0;
    }
  
    return 0;
}
#pragma mark --- 设置分区内cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //热门专辑
    if (indexPath.section == 0) {
        return 122;
    }
    //猜你喜欢
    if (indexPath.section == 1) {
        return ScreenHeight / 4;
    }
    //精品阅读
    if (indexPath.section == 2) {
        return 120;
    }
    //热门活动
    if (indexPath.section == 3) {
        return 120;
    }
    //好豆达人
    if (indexPath.section == 4) {
        return 0;
    }
    return 50;
}

#pragma mark---设置分区标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 4) {
        
        return 0;
        
    }
    else
    {
        return 49;
    }
    return 0;
}
#pragma mark --- 设置分区尾视图
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}



#pragma mark --- 自定义分区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MLWWWFourImageView *headeView = [[MLWWWFourImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 49)];

    //右侧跳转界面按钮:
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(ScreenWidth - 35, 13, 16, 16);
    [self.rightButton setImage:[UIImage imageNamed:@"ww_into"] forState:UIControlStateNormal];
    //为按钮添加点击方法:
    [self.rightButton addTarget:self action:@selector(didClickedRightButton:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton.tag = 1000 + section;
    [headeView addSubview:self.rightButton];
    
    if (section == 0) {
        
        headeView.leftImageView.image = [UIImage imageNamed:@"ww_hot"];
        headeView.rightLabel.text = @"热门专辑";
        headeView.rightLabel.font = [UIFont systemFontOfSize:18];
        
    }
    if (section == 1) {
        
        headeView.leftImageView.image = [UIImage imageNamed:@"ww_like"];
        headeView.rightLabel.text = @"猜你喜欢";
        headeView.rightLabel.font = [UIFont systemFontOfSize:18];
        
    }
    if (section == 2) {
        
        headeView.leftImageView.image = [UIImage imageNamed:@"ww_read"];
        headeView.rightLabel.text = @"精品阅读";
        headeView.rightLabel.font = [UIFont systemFontOfSize:18];
        
    }
    if (section == 3) {
        
        headeView.leftImageView.image = [UIImage imageNamed:@"ww_activity"];
        headeView.rightLabel.text = @"热门活动";
        headeView.rightLabel.font = [UIFont systemFontOfSize:18];
        
    }
    if (section == 4) {
        
        headeView.leftImageView.image = [UIImage imageNamed:@"ww_daren"];
        headeView.rightLabel.text = @"厨房达人";
        headeView.rightLabel.font = [UIFont systemFontOfSize:18];
        
    }
    return headeView;
    
}







#pragma mark --- cell 重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //热门专辑
    if (indexPath.section == 0) {
        
        MLWWWHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mLWWWHotTableViewCell"];
        
        cell.hotModel = self.hotModel;
               
        return cell;
    }
    if (indexPath.section == 1) {
        MLWLXPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mlwLXPersonTableViewCell"];
      
        cell.scrollDataSource = self.perArray;
        

        
        return cell;
    }
    if (indexPath.section == 2) {
        MLWLXReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mlwReadTableViewCell"];
       //为控件赋值:
       
        cell.titleLabel.text = [[self.readModel.smallModelArray objectAtIndex:indexPath.row] Title];
        cell.contentLabel.text = [[self.readModel.smallModelArray objectAtIndex:indexPath.row] Intro];
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:[[self.readModel.smallModelArray objectAtIndex:indexPath.row] Img]]placeholderImage:[UIImage imageNamed:@"placeholder"]];

        
        return cell;
    }
    if (indexPath.section == 3) {
        MLWLXEventTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mlwLXEventTableViewCell"];
        //为控件赋值:
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:[[self.eventModel.smallModelArray objectAtIndex:indexPath.row] Img]]placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.titleLabel.text = [[self.eventModel.smallModelArray objectAtIndex:indexPath.row] Title];
        cell.contentLabel.text = [[self.eventModel.smallModelArray objectAtIndex:indexPath.row] Intro];
     
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"txt"];
        return cell;
    }
    return nil;
}



-(void)getDataSource
{
    
    [MLWNetTool GetNetWithUrl:@"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=4e1d878cd79d8c5c35eecf6f6b39af69&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C44C64D22-DBB4-49AD-BB93-DC50565EFC46%7C2BD8F6CD-B01C-402F-87D8-D7B593B97EE7&format=json&loguid=&method=Index.index&nonce=1465262448&sessionid=1465262367&signmethod=md5&timestamp=1465262448&uuid=075250bef84b0f93aa0cd8bacffe220f&v=2&vc=49&vn=v6.1.1" body:nil header:nil response:MLWJSON success:^(id result) {
        
        [LTArchiver archiverObject:result ByKey:@"cookBook" WithPath:@"cookBook.plist"];
        
        NSDictionary *dic = [(NSDictionary *)result objectForKey:@"result"];
        
        for (NSDictionary *dict in [[dic objectForKey:@"recipe"] objectForKey:@"list"]) {
            
            MLWWWCyscrollModel *model = [[MLWWWCyscrollModel alloc]initWithDic:dict];
            
            [self.scrollDataSource addObject:model];
            
            [model release];
            
            [self getScorllView];
            
        }
        
        //热门专辑
        NSDictionary *hotDic = [dic objectForKey:@"album"];
        
       self.hotModel = [[MLWWWHotModel alloc]initWithDic:hotDic];
        
        [_hotModel release];
        //猜你喜欢
        NSDictionary *personDic = [dic objectForKey:@"person"];
        
        self.personModel = [[MLWLXPersonModel alloc] initWithDic:personDic];;
        
        self.perArray = [NSMutableArray array];
//        [self.perArray addObject:self.personModel];
        self.perArray = self.personModel.smallModelArray;
        
        [_personModel release];
        
        //精品阅读
        NSDictionary *readDic = [dic objectForKey:@"read"];
       
        self.readModel = [[MLWLXReadModel alloc] initWithDic:readDic];
        [_readModel release];
        //热门活动:
        NSDictionary *eventDic = [dic objectForKey:@"event"];
        self.eventModel = [[MLWLXEventModel alloc] initWithDic:eventDic];
        [_eventModel release];
        
        [self.mainTableView reloadData];
        
                
    } failure:^(NSError *error) {
        
        NSLog(@"%@", error);
        
        NSDictionary *dic = [LTArchiver unarchiverObjectByKey:@"cookBook" WithPath:@"cookBook.plist"];
        for (NSDictionary *dict in [[dic objectForKey:@"recipe"] objectForKey:@"list"]) {
            
            MLWWWCyscrollModel *model = [[MLWWWCyscrollModel alloc]initWithDic:dict];
            
            [self.scrollDataSource addObject:model];
            
            [model release];
            
            [self getScorllView];
            
        }
        
        //热门专辑
        NSDictionary *hotDic = [dic objectForKey:@"album"];
        
        self.hotModel = [[MLWWWHotModel alloc]initWithDic:hotDic];
        
        [_hotModel release];
        //猜你喜欢
        NSDictionary *personDic = [dic objectForKey:@"person"];
        
        self.personModel = [[MLWLXPersonModel alloc] initWithDic:personDic];;
        
        self.perArray = [NSMutableArray array];
        //        [self.perArray addObject:self.personModel];
        self.perArray = self.personModel.smallModelArray;
        
        [_personModel release];
        
        //精品阅读
        NSDictionary *readDic = [dic objectForKey:@"read"];
        
        self.readModel = [[MLWLXReadModel alloc] initWithDic:readDic];
        [_readModel release];
        //热门活动:
        NSDictionary *eventDic = [dic objectForKey:@"event"];
        self.eventModel = [[MLWLXEventModel alloc] initWithDic:eventDic];
        [_eventModel release];
        
        [self.mainTableView reloadData];
        
//        NSDictionary *dic = [LTArchiver unarchiverObjectByKey:@"cookBook" WithPath:@"cookBook.plist"];
//        for (NSDictionary *dict in [[dic objectForKey:@"recipe"] objectForKey:@"list"]) {
//            
//            MLWWWCyscrollModel *model = [[MLWWWCyscrollModel alloc]initWithDic:dict];
//            
//            [self.scrollDataSource addObject:model];
//            
//            [model release];
//            
//            [self getScorllView];
//            
//        }
//        //热门专辑
//        NSDictionary *hotDic = [dic objectForKey:@"album"];
//        
//        self.hotModel = [[MLWWWHotModel alloc]initWithDic:hotDic];
//        
//        [_hotModel release];
//        
//        [self.mainTableView reloadData];
        
    }];
    
}

- (void)didClickedRightButton:(UIButton *)section
{
    MLWAlbumViewController *avc = [[MLWAlbumViewController alloc] init];
    MLWPersonViewController *pvc = [[MLWPersonViewController alloc] init];
    MLWEventViewController *evc = [[MLWEventViewController alloc] init];
    MLWReadViewController *rvc = [[MLWReadViewController alloc] init];
    MLWVipViewController *vvc = [[MLWVipViewController alloc] init];
    
    switch (section.tag) {
        case 1000:
            [self.navigationController pushViewController:avc animated:YES];
            break;
            
            case 1001:
            [self.navigationController pushViewController:pvc animated:YES];
            break;
        case 1002:
            [self.navigationController pushViewController:rvc animated:YES];

            break;
            
        case 1003:
            [self.navigationController pushViewController:evc animated:YES];
            break;
            
            case 1004:
            [self.navigationController pushViewController:vvc animated:YES];
            break;
            
        default:
            break;
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
